home *** CD-ROM | disk | FTP | other *** search
- Path: news.logicon.com!newsmaster@klee
- From: kkolda@logicon.com (Kenneth D. Kolda)
- Newsgroups: comp.lang.c++
- Subject: Re: Is WATCOM 10.5 a broken compiler or am I stupid?
- Date: 29 Mar 1996 18:29:39 GMT
- Organization: Logicon Operating Systems
- Message-ID: <4jha6j$pf4@piper.logicon.com>
- References: <315A8A29.613B4EBA@rz.tu-ilmenau.de>
- NNTP-Posting-Host: 137.51.122.161
- Mime-Version: 1.0
-
- In article <315A8A29.613B4EBA@rz.tu-ilmenau.de>, ai108@rz.tu-ilmenau.de
- says...
- >
- >Hi.
- >
- >Yesterday I came across a strange error caused by following code:
- >
- >typedef struct cstringTEMP
- >{
- > char len;
- > char data[255];
- > public:
- > cstringTEMP & operator=(char *);
- >} cstring;
- >
- >char pchar2cstring(cstring &s, char *c)
- >// Returns 1 for "c to long".
- >// Returns 0 for success.
- >{
- > unsigned l=strlen(c);
- > if( l>255)
- > return 1;
- > s.len=(char )l;
- > memcpy(s.data,c,l);
- > return 0;
- >}
- >
- >cstring & cstring::operator=(char *s)
- >{
- > if( pchar2cstring(*this,s))
- > len=0;
- > return *this;
- >}
- >
- >
- >
- >
- >AND THIS IS THE CODE I TRIED TO USE:
- >
- > cstring cs="test";
- >
- >
- >IT GAVE THIS ERROR-MESSAGE: (I CUT OUT THE LINE NUMBERS AND THE FILENAME
- >TO SAVE SPACE.)
- >Error! E400: (col 13) cannot convert right expression for initialization
- >Note! N643: (col 13) cannot convert argument 1 defined in:
- >Note! N630: (col 13) source conversion type is "char *"
- >Note! N631: (col 13) target conversion type is "cstringTEMP const &"
- >
- >CHANGING THE LINE ABOVE TO:
- >
- >cstring cs;
- >cs="test";
- >
- >
- >AND THE ERROR WAS NOT LONGER THERE.
- >
- >And now are my questions
- >1.) Is this all my fault or a bug in the WATCOM C++ compiler.
- >2.) If it is my fault how do I have to change the declaration.
- >
- >
- >Thanks in advance
- >
- >Lars
-
- No, you're compiler's not crazy. When you declare and initialize a
- variable in a single statement, C++ assumes you want to use a contructor
- for that initialization. So, if you redeclare cstring as:
-
- struct cstring {
- char len;
- char data[255];
- cstring& operator=(const char*);
- cstring(const char* s) { *this = s; }; // A cstring constructor
- }
-
-
- then your initialization will work.
-
- Ken Kolda
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-